/**
 *
 */
package com.ybkj.daijia.api.driver.v1;

import com.ybkj.daijia.api.driver.ErrorConstant;
import com.ybkj.daijia.api.driver.SecurityUtils;
import com.ybkj.daijia.api.driver.transport.ResultTransport;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.service.DriverService;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 司机设备
 * @author bo
 *
 */
@Controller
@RequestMapping(value = "driver/api/rest/v1")
public class DeviceController {

    private static final Logger logger = LoggerFactory.getLogger(DeviceController.class);

    @Autowired
    private DriverService driverService;

    @RequestMapping(value = "bindDevice", method = RequestMethod.POST)
    public @ResponseBody
    ResultTransport bindDevice(Long driverID, String deviceType, String channelID, String userID,
        String appKey, String token, String timestamp) {

        logger.debug(
            "recv bindDevice -- driverID:{},deviceType:{},channelID:{},userID:{},appKey:{},token:{},timestamp:{}",
            driverID, deviceType, channelID, userID, appKey, token, timestamp);

        if (null == driverID || StringUtils.isBlank(userID) || SecurityUtils
            .isBlank(appKey, token, timestamp)) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

        if (!StringUtils.isNumeric(timestamp) || !SecurityUtils
            .checkTimestamp(Long.parseLong(timestamp))) {
            return ResultTransport.getErrorBy(ErrorConstant.TIMESTAMP_ERROR);
        }

        List<String> params = new LinkedList<String>();
        params.add(String.valueOf(driverID));
        params.add(deviceType);
        params.add(channelID);
        params.add(userID);
        params.add(timestamp);
        params.add(appKey);

        if (!SecurityUtils.checkToken(params, token)) {
            return ResultTransport.getErrorBy(ErrorConstant.TOKEN_ERROR);
        }

        try {
            Driver driver = driverService.findOne(driverID);

            if (null == driver) {
                return ResultTransport.getErrorBy(ErrorConstant.DRIVER_NOT_EXISTS);
            }

            if (driver.getIsFreeze()) { //帐号被冻结
                return ResultTransport.getErrorBy(ErrorConstant.USER_IS_FREEZE_ERROR);
            }

            //清空channelID userID相同的司机
            driverService.updateDevice(channelID, userID);

            driver.setDeviceType(deviceType);
            driver.setUserID(userID);

            driverService.updatePushByPrimaryKey(driver);

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
            instance.setData(driver);

            return instance;

        } catch (Exception e) {
            logger.error("api v1 driver bindDevice error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }

    }

}
